package com.ely.coupon.activity.validation.constraint;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ely.coupon.activity.entity.CouponActivity;
import com.ely.coupon.activity.mapper.CouponActivityMapper;
import com.ely.coupon.activity.validation.CheckName;
import com.ely.coupon.activity.validation.CheckType;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

@Component
public class CheckNameValidator implements ConstraintValidator<CheckName, String> {
    @Resource
    private CouponActivityMapper couponActivityMapper;

    @Override
    public boolean isValid(String name, ConstraintValidatorContext context) {
        if (ObjectUtils.isEmpty(name)) {
            return false;
        }
        QueryWrapper<CouponActivity> qw = new QueryWrapper<>();
        qw.lambda().eq(CouponActivity::getCouponName, name);
        return ObjectUtils.isEmpty(couponActivityMapper.selectOne(qw));
    }
}
